-- STARTING OVER FROM SCRATCH!!  WFWW 2.0! --

--credit line

--rActionButtonStyler texture lines (or see about how to register these buttons with rABS???)

local ObjectiveTrackerFrame = ObjectiveTrackerFrame
local minimizeButton = ObjectiveTrackerFrame.HeaderMenu.MinimizeButton
local otfHeader = ObjectiveTrackerFrame.HeaderMenu
local otfQuests = ObjectiveTrackerBlocksFrame.QuestHeader
local otfAchievements = ObjectiveTrackerBlocksFrame.AchievementHeader
local otfScenarios = ObjectiveTrackerBlocksFrame.ScenarioHeader
local otfBonus = BONUS_OBJECTIVE_TRACKER_MODULE.Header
local ScenarioStageBlock = ScenarioStageBlock
local LSM = LibStub("LibSharedMedia-3.0")
local widgetLists = AceGUIWidgetLSMlists
local GameTooltip = GameTooltip
local positions = {"TOP", "TOPRIGHT", "TOPLEFT",
					"BOTTOM", "BOTTOMRIGHT", "BOTTOMLEFT",
					"RIGHT", "LEFT", "CENTER"}
local fontFlags = {"None", "Outline", "Monochrome Outline"}	--, "Monochrome"}
local _,class = UnitClass("player")
local db, font, classColor
local WFWWSkinnedHeaders = {}
local WFWWSkinnedLines = {}
local tinsert = tinsert
local MoveObjectiveTracker, SetSize, Appearance, FontChanges

local OTFContainer = CreateFrame("Frame", "OTFContainer", UIParent)

local defaults = {
	bg = "Solid",
	border = "Blizzard Tooltip",
	borderWidth = 16,
	inset = 5,
	anchorPoint = "TOPRIGHT",
	xOffset = "-15",
	yOffset = "-250",
	height = 600,
	bgColor = {r = .1, g = .1, b = .1, a = .6},
	borderColor = {r = 1, g = 1, b = 1},
	borderAlpha = .8,
	classBorder = false,
	font = "Friz Quadrata TT",
	fontSize = 12,
	fontFlag = "None",
	questSpacing = 10,
	lineSpacing = 4,
	disableShadow = false,
	scale = 1,
	hideFrame = false,
	instance = false,
	petBattle = false,
	keybind = "",
}

local options = {
	name = "Who Framed Watcher Wabbit?",
	type = "group",
	args = {
		spacer = {
			name = " ",
			type = "description",
			width = "full",
			order = 0,
		},
		title1 = {
			name = "Size and Position",
			type = "header",
			order = .5,
		},
		anchor = {
			name = "Screen Anchor",
			desc = "What side of the screen you would like the WatchFrame located at. The tracker will also collapse in this direction.",
			type = "select",
			values = positions,
			get = function()
						for k, v in pairs(positions) do
							if db.anchorPoint == v then
								return k
							end
						end
					end,
			set = function(_, anchor)
						db.anchorPoint = positions[anchor]
						db.xOffset = "0"
						db.yOffset = "0"
						MoveObjectiveTracker()
					end,
			order = 1,
		},
		xOffset = {
			name = "X Offset",
			desc = "Offsets from the watch frame's anchor left/right",
			type = "input",
			get = function() return db.xOffset end,
			set = function(_,xOffset)
						if xOffset == nil or xOffset == "" then
							xOffset = "0"
						end
						db.xOffset = xOffset
						MoveObjectiveTracker()
					end,
			order = 2,
		},
		yOffset = {
			name = "Y Offset",
			desc = "Offsets from the watch frame's anchor up/down",
			type = "input",
			get = function() return db.yOffset end,
			set = function(_,yOffset)
						if yOffset == nil or yOffset == "" then
							yOffset = "0"
						end
						db.yOffset = yOffset
						MoveObjectiveTracker()
					end,
			order = 3,
		},
		scale = {
			name = "WatchFrame scale",
			desc = "The scale of the entire tracker frame.",
			type = "range",
			min = .5,
			max = 2,
			step = .05,
			get = function() return db.scale end,
			set = function(_, scale) 
						db.scale = scale
						SetSize()
					end,
			order = 4,
		},
		height = {
			name = "WatchFrame height",
			desc = "The maximum height of the tracker frame. A larger height can display more quests.",
			type = "range",
			min = 200,
			max = 1000,
			step = 5,
			get = function() return db.height end,
			set = function(_,height)
						db.height = height
						SetSize()
					end,
			order = 5,
		},
		spacer1 = {
			name = " ",
			type = "description",
			width = "full",
			order = 5.4,
		},
		title2 = {
			name = "Font Options",
			type = "header",
			order = 5.5,
		},
		font = {
			name = "Font",
			desc = "Select the font for the watch frame.",
			type = "select",
			dialogControl = "LSM30_Font",
			values = widgetLists.font,
			get = function()
					return db.font
				end,
			set = function(self, font)
					db.font = font
					FontChanges()
				end,
			order = 6,
		},
		fontSize = {
			name = "Text Size",
			desc = "Change the font size of the watch frame.",
			type = "range",
			min = 6,
			max = 24,
			step = 1,
			get = function() return db.fontSize end,
			set = function(_,size) 
						db.fontSize = size
						FontChanges()
					end,
			order = 7,
		},
		fontFlag = {
			name = "Font Flag",
			desc = "Set how to alter the watch frame's font.",
			type = "select",
			values = fontFlags,
			get = function()
						for k, v in pairs(fontFlags) do
							if db.fontFlag == v then
								return k
							end
						end
					end,
			set = function(_,key)
						db.fontFlag = fontFlags[key]
						FontChanges()
					end,
			order = 8,
		},
		disableShadow = {
			name = "Disable Font Shadow",
			desc = "If checked, the shadow effect on each line will be hidden.",
			type = "toggle",
			get = function() return db.disableShadow end,
			set = function()
						db.disableShadow = not db.disableShadow
						FontChanges()
					end,
			order = 8.5,
		},
--[[		questSpacing = {
			name = "Quest Spacing",
			desc = "Change the spacing between quests.",
			type = "range",
			min = 0,
			max = 12,
			step = 1,
			get = function() return db.questSpacing end,
			set = function(_,size)
						db.questSpacing = size
						DEFAULT_OBJECTIVE_TRACKER_MODULE.blockOffsetY = -db.questSpacing
						ObjectiveTracker_Update()
					end,
			order = 8.51,
		},]]
		--[[lineSpacing = {
			name = "Line Spacing",
			desc = "Change the spacing between lines.",
			type = "range",
			min = 0,
			max = 10,
			step = 1,
			get = function() return db.lineSpacing end,
			set = function(_,size)
						db.lineSpacing = size
						otfQuests.module.linespacing = db.lineSpacing
						otfAchievements.module.linespacing = db.lineSpacing
						--ObjectiveTracker_Update()
					end,
			order = 8.52,
		},]]
		spacer2 = {
			name = " ",
			type = "description",
			width = "full",
			order = 8.6,
		},
		title3 = {
			name = "Texture Options",
			type = "header",
			order = 8.7,
		},
		bg = {
			name = "Background",
			desc = "Texture to use as the tracker's background.",
			type = "select",
			dialogControl = "LSM30_Background",
			values = widgetLists.background,
			get = function()
					return db.bg
				end,
			set = function(self, bg)
					db.bg = bg
					Appearance()
				end,
			order = 9,
		},
		bgColor = {
			name = "Background Color/Alpha",
			desc = "The background color and transparency.\n\nIf you wish to use a custom background texture and do not want it colored, set this to white.",
			type = "color",
			hasAlpha = true,
			width = "double",
			get = function() return db.bgColor.r, db.bgColor.g, db.bgColor.b, db.bgColor.a end,
			set = function(_,r,g,b,a)
					db.bgColor.r = r
					db.bgColor.g = g
					db.bgColor.b = b
					db.bgColor.a = a
					Appearance()
				end,
			order = 10,
		},
		border = {
			name = "Border",
			desc = "Texture for the watch frame's border.",
			type = "select",
			dialogControl = "LSM30_Border",
			values = widgetLists.border,
			get = function()
					return db.border
				end,
			set = function(self, border)
					db.border = border
					Appearance()
				end,
			order = 11,
		},
		borderColor = {
			name = "Border Color",
			desc = "Color of the watch frame's border.",
			type = "color",
			disabled = function() return db.classBorder end,
			get = function() return db.borderColor.r, db.borderColor.g, db.borderColor.b end,
			set = function(_,r,g,b)
						db.borderColor.r = r
						db.borderColor.g = g
						db.borderColor.b = b
						Appearance()
					end,
			order = 12,
		},
		classBorder = {
			name = "Class Colored Border",
			desc = "Color the watch frame's border by your class.",
			type = "toggle",
			get = function() return db.classBorder end,
			set = function()
						db.classBorder = not db.classBorder
						Appearance()
					end,
			order = 12.5,
		},
		borderAlpha = {
			name = "Border Alpha",
			desc = "Transparency of the border.",
			type = "range",
			min = 0,
			max = 1,
			step = .05,
			get = function() return db.borderAlpha end,
			set = function(_, borderAlpha) 
						db.borderAlpha = borderAlpha
						Appearance()
					end,
			order = 13,
		},
		borderWidth = {
			name = "Border Width",
			desc = "Width of the border.",
			type = "range",
			min = 1,
			max = 24,
			step = .5,
			get = function() return db.borderWidth end,
			set = function(_, borderWidth) 
						db.borderWidth = borderWidth
						Appearance()
					end,
			order = 13.5,
		},
		inset = {
			name = "Inset",
			desc = "The inset of the background from the border texture.",
			type = "range",
			min = 0,
			max = 10,
			step = .5,
			get = function() return db.inset end,
			set = function(_, inset) 
						db.inset = inset
						Appearance()
					end,
			order = 14,
		},
		spacer3 = {
			name = " ",
			type = "description",
			width = "full",
			order = 14.4,
		},
		title4 = {
			name = "Miscellaneous",
			type = "header",
			order = 14.5,
		},
		hideFrame = {
			name = "Hide frame when empty",
			desc = "Hide the watch frame when no quests or achievements are being tracked.",
			type = "toggle",
			get = function() return db.hideFrame end,
			set = function()
						db.hideFrame = not db.hideFrame
						if db.hideFrame and GetNumTrackedAchievements() == 0 and GetNumQuestWatches() == 0 then
							OTFContainer:SetAlpha(0)
							minimizeButton:EnableMouse(false)
						else
							OTFContainer:SetAlpha(1)
							minimizeButton:EnableMouse(true)
						end
					end,
			order = 15,
		},
		instance = {
			name = "Collapse in instance",
			desc = "Automatically collapses the watch frame when entering an instance.",
			type = "toggle",
			get = function() return db.instance end,
			set = function()
						db.instance = not db.instance
						if db.instance then
							OTFContainer:RegisterEvent("PLAYER_ENTERING_WORLD")
						else
							OTFContainer:UnregisterEvent("PLAYER_ENTERING_WORLD")
						end
					end,
			order = 15.2,
		},
		petBattle = {
			name = "Collapse in pet battle",
			desc = "Automatically collapses the watch frame when entering a pet battle.",
			type = "toggle",
			get = function() return db.petBattle end,
			set = function()
						db.petBattle = not db.petBattle
						if db.petBattle then
							OTFContainer:RegisterEvent("PET_BATTLE_OPENING_START")
							OTFContainer:RegisterEvent("PET_BATTLE_CLOSE")
						else
							OTFContainer:RegisterEvent("PET_BATTLE_OPENING_START")
							OTFContainer:RegisterEvent("PET_BATTLE_CLOSE")
						end
					end,
			order = 15.5,
		},
		keybind = {
			name = "Keybinding",
			desc = "Key to minimize/maximize the tracker.",
			type = "keybinding",
			get = function() return db.keybind end,
			set = function(_,key)
						SetOverrideBinding(OTFContainer, false, db.keybind, nil)	--clear old binding
						if key ~= "" then	--set new
							SetOverrideBindingClick(OTFContainer, false, key, "WFWWkeybindButton")
						end
						db.keybind = key
					end,
			order = 15.7,
		},
		spacer4 = {
			name = " ",
			type = "description",
			width = "full",
			order = 16,
		},
		divider = {
			name = "Profile Management",
			type = "header",
			order = 17,
		},
		charSpec = {
			name = "Character specific settings",
			desc = "Have this character use their own profile.  If checked, any changes made will not affect other characters.\n\n|c00E30016WARNING:|r Your UI will be reloaded in the process!",
			type = "toggle",
			width = "full",
			confirm = true,
			get = function() return WFWWPCDB.charSpec end,
			set = function()
						WFWWPCDB.charSpec = not WFWWPCDB.charSpec
						ReloadUI()
					end,
			order = 18,
		},
		copyProfile = {
			name = "Copy from Default",
			desc = "Copy all settings from the default, global profile to this character's profile.  This will not effect other characters' specific profiles.\n\n|c00E30016WARNING:|r Your UI will be reloaded in the process!",
			type = "execute",
			confirm = true,
			disabled = function() return not WFWWPCDB.charSpec end,
			func = function()
						WFWWPCDB = WFWWDB
						WFWWPCDB.charSpec = true
						ReloadUI()
					end,
			order = 19,
		},
		resetProfile = {
			name = "Profile Reset",
			desc = "Reset this profile back to the out-of-the-box settings.  If you reset the character specific profile, the global profile will be untouched, and vice versa.  This will not effect other character specific profiles.\n\n|c00E30016WARNING:|r Your UI will be reloaded in the process!",
			type = "execute",
			confirm = true,
			func = function()
						if WFWWPCDB.charSpec then
							WFWWPCDB = {charSpec = true}
						else
							WFWWDB = {}
						end
						ReloadUI()
					end,
			order = 20,
		},
	},
}


function MoveObjectiveTracker()	--declared local at top of file
	OTFContainer:ClearAllPoints()
	OTFContainer:SetPoint(db.anchorPoint, UIParent, db.anchorPoint, db.xOffset, db.yOffset)
end

function SetSize()
	OTFContainer:SetHeight(db.height)
	OTFContainer:SetWidth(280)
	OTFContainer:SetScale(db.scale)
end

local function ExpandObjectiveTracker()
	if ObjectiveTrackerFrame.collapsed then
		OTFContainer:SetHeight(db.height)
		OTFContainer:SetWidth(280)
		ObjectiveTracker_MinimizeButton_OnClick()	--default function called by OnClick
	end

	if GetNumTrackedAchievements() == 0 and GetNumQuestWatches() == 0 and GetNumAutoQuestPopUps() == 0 then
		--if our tracker is empty, still show "Objectives" text
		otfHeader.Title:Show()
	elseif not WFWWPCDB.collapsed then	--if not empty and not collapsed
		otfHeader.Title:Hide()
	end	
end

local function CollapseObjectiveTracker()
	if not ObjectiveTrackerFrame.collapsed then
		OTFContainer:SetHeight(30)
		OTFContainer:SetWidth(150)
		ObjectiveTracker_MinimizeButton_OnClick()	--default function called by OnClick
	end
end

function Appearance()
	OTFContainer:SetBackdrop({
			bgFile = LSM:Fetch("background", db.bg),
			edgeFile = LSM:Fetch("border", db.border),
			edgeSize = db.borderWidth,
			insets = {left=db.inset,right=db.inset,top=db.inset,bottom=db.inset}
		})
	OTFContainer:SetBackdropColor(db.bgColor.r,db.bgColor.g,db.bgColor.b,db.bgColor.a)
	if db.classBorder then
		OTFContainer:SetBackdropBorderColor(classColor.r,classColor.g,classColor.b,db.borderAlpha)
	else
		OTFContainer:SetBackdropBorderColor(db.borderColor.r,db.borderColor.g,db.borderColor.b,db.borderAlpha)
	end
end

function FontChanges()
	font = LSM:Fetch("font", db.font)
	local shadow = db.disableShadow and 0 or 1
	
	otfHeader.Title:SetFont(font, db.fontSize, db.fontFlag)
	otfHeader.Title:SetShadowColor(0,0,0,shadow)
	otfQuests.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfQuests.Text:SetShadowColor(0,0,0,shadow)
	otfAchievements.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfAchievements.Text:SetShadowColor(0,0,0,shadow)
	otfScenarios.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfScenarios.Text:SetShadowColor(0,0,0,shadow)
	otfBonus.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfBonus.Text:SetShadowColor(0,0,0,shadow)
	ScenarioStageBlock.Stage:SetFont(font, db.fontSize+5, db.fontFlag)
	
	for k,v in pairs(WFWWSkinnedHeaders) do
		v:SetFont(font, db.fontSize, db.fontFlag)
		v:SetShadowColor(0,0,0,shadow)
	end
	for k,v in pairs(WFWWSkinnedLines) do
		v.Text:SetFont(font, db.fontSize, db.fontFlag)
		v.Text:SetShadowColor(0,0,0,shadow)
		if v.Dash then
			v.Dash:SetFont(font, db.fontSize, db.fontFlag)
			v.Dash:SetShadowColor(0,0,0,shadow)
		end
	end
	
--	ObjectiveTracker_Update()
end

local function SetUpContainer()
	--nUI code here if desired
	
	MoveObjectiveTracker()
	SetSize()
	Appearance()
	--SkinButtons()	--add later for rABS or Masque(?) support
	
	CreateFrame("Button", "WFWWkeybindButton")
	WFWWkeybindButton:SetScript("OnClick", function()
			if ObjectiveTrackerFrame.collapsed then
				ExpandObjectiveTracker()
			else
				CollapseObjectiveTracker()
			end
		end)
	if db.keybind ~= "" then
		SetOverrideBindingClick(OTFContainer, false, db.keybind, "WFWWkeybindButton")
	end
	
--	DEFAULT_OBJECTIVE_TRACKER_MODULE.blockOffsetY = -db.questSpacing
	--otfQuests.module.linespacing = db.lineSpacing
	--otfAchievements.module.linespacing = db.lineSpacing
		
	--make sure fonts for other modules are good, too
	
	FontChanges()
	local function HookForUpdates(module, block, objectiveKey, text, lineType, useFullHeight, hideDash, colorStyle)
		local header = block.HeaderText
		local line = block.lines[objectiveKey]
		--line.Text:SetWordWrap(false)
		--print(block.currentLine.Text:GetText())
		if header and not header.wfwwSkinned then
			header:SetFont(font, db.fontSize, db.fontFlag)
			header:SetShadowColor(0,0,0,db.disableShadow and 0 or 1)
			tinsert(WFWWSkinnedHeaders, header)
			header.wfwwSkinned = true
		end
		if line and not line.wfwwSkinned then
			line.Text:SetFont(font, db.fontSize, db.fontFlag)
			line.Text:SetShadowColor(0,0,0,db.disableShadow and 0 or 1)
			if line.Dash then
				line.Dash:SetFont(font, db.fontSize, db.fontFlag)
				line.Dash:SetShadowColor(0,0,0,db.disableShadow and 0 or 1)
			end
			tinsert(WFWWSkinnedLines, line)
			line.wfwwSkinned = true
		end
	end
	for i = 1, #ObjectiveTrackerFrame.MODULES do
		hooksecurefunc(ObjectiveTrackerFrame.MODULES[i], "AddObjective", HookForUpdates)
	end
	hooksecurefunc(SCENARIO_TRACKER_MODULE, "AddObjective", HookForUpdates)
	
	ObjectiveTrackerFrame:SetParent(OTFContainer)
	ObjectiveTrackerFrame:SetClampedToScreen(false)
	ObjectiveTrackerFrame:ClearAllPoints()
	ObjectiveTrackerFrame.ClearAllPoints = function() end
	ObjectiveTrackerFrame:SetPoint("TOPLEFT", 35, -5)
	ObjectiveTrackerFrame:SetPoint("BOTTOMRIGHT", -10, 5)
	ObjectiveTrackerFrame.SetPoint = function() end

	otfQuests.animateReason = 0
	otfAchievements.animateReason = 0
	
	minimizeButton:Show()
	minimizeButton.Hide = function() end
	otfHeader:Show()
	otfHeader.Hide = function() end
	
	hooksecurefunc("ObjectiveTracker_MinimizeButton_OnClick", function()
		WFWWPCDB.collapsed = ObjectiveTrackerFrame.collapsed
	end)
	
	if WFWWPCDB.collapsed then	--recall if watchframe was collapsed on last logout
		CollapseObjectiveTracker()
	else
		ExpandObjectiveTracker()
	end
	
	minimizeButton:RegisterForClicks("AnyDown")
	minimizeButton:SetScript("OnClick", function(self,btn)
		if IsShiftKeyDown() then
			ToggleAchievementFrame()
		elseif IsAltKeyDown() then
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
		elseif btn == "RightButton" then
			ToggleFrame(WorldMapFrame)
		else
			if not WFWWPCDB.collapsed then	--if the container's not collapsed, collapse it
				CollapseObjectiveTracker()
			else
				ExpandObjectiveTracker()
			end
		end
	end)
		
	minimizeButton:SetScript("OnEnter", function(self)
			GameTooltip:SetOwner(self, "ANCHOR_TOP")
			GameTooltip:AddLine("Who Framed Watcher Wabbit?",1,1,1)
			if ObjectiveTrackerFrame.collapsed then
				GameTooltip:AddLine("Left-click to expand the objectives tracker.")
			else
				GameTooltip:AddLine("Left-click to minimize the objectives tracker.")
			end
			GameTooltip:AddLine("Right-click to toggle the quest log.")
			GameTooltip:AddLine("Shift-click to toggle the achievements window.")
			GameTooltip:AddLine("Alt-click to open the Who Framed Watcher Wabbit? options.")
			GameTooltip:Show()
		end)
	minimizeButton:SetScript("OnLeave", function() GameTooltip:Hide() end)
	
--	ObjectiveTracker_Update()
end

local function SetUpDB()
	if WFWWPCDB.charSpec then
		--set defaults if new charSpec DB
		for k,v in pairs(defaults) do
			if type(WFWWPCDB[k]) == "nil" then
				WFWWPCDB[k] = v
			end
		end
		db = WFWWPCDB
	else
		db = WFWWDB
	end
end

local function Initialize()
	WFWWDB = WFWWDB or {}
	WFWWPCDB = WFWWPCDB or {}
		if WFWWPCDB.charSpec == nil then
			WFWWPCDB.charSpec = false
		end
		if WFWWPCDB.collapsed == nil then
			WFWWPCDB.collapsed = false
		end
	for k,v in pairs(defaults) do
	    if type(WFWWDB[k]) == "nil" then
	        WFWWDB[k] = v
	    end
	end
	SetUpDB()
	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("Who Framed Watcher Wabbit?", options)
	OTFContainer.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Who Framed Watcher Wabbit?", "Who Framed Watcher Wabbit?")
	SlashCmdList["WFWW"] = function()
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
		end
	SLASH_WFWW1 = "/wfww"
	
	classColor = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
	if CUSTOM_CLASS_COLORS then
		CUSTOM_CLASS_COLORS:RegisterCallback(function()
				if db.classBorder then
					Appearance()
				end
			end)
	end
	
	if not db.instance then
		OTFContainer:UnregisterEvent("PLAYER_ENTERING_WORLD")
	end
	if db.petBattle then
		OTFContainer:RegisterEvent("PET_BATTLE_OPENING_START")
		OTFContainer:RegisterEvent("PET_BATTLE_CLOSE")
	end
	OTFContainer:RegisterEvent("QUEST_AUTOCOMPLETE")
	OTFContainer:RegisterEvent("QUEST_WATCH_LIST_CHANGED")
	OTFContainer:RegisterEvent("TRACKED_ACHIEVEMENT_LIST_CHANGED")
	local notCollapsed
	OTFContainer:SetScript("OnEvent", function(_, event, ...)
			if event == "PLAYER_ENTERING_WORLD" then
				if IsInInstance() and not WFWWPCDB.collapsed then
					--if you go in the instance and the WF isn't collapsed...
					CollapseObjectiveTracker()	--collapse
				end
			elseif event == "PET_BATTLE_OPENING_START" and not WFWWPCDB.collapsed then
				notCollapsed = true	--if it was open before pet battle
				CollapseObjectiveTracker()
			elseif event == "PET_BATTLE_CLOSE" and notCollapsed then
				ExpandObjectiveTracker()	--open again after pet battle if it was open before
				notCollapsed = false
			elseif event == "QUEST_WATCH_LIST_CHANGED" or event == "TRACKED_ACHIEVEMENT_LIST_CHANGED" then
				if GetNumTrackedAchievements() == 0 and GetNumQuestWatches() == 0 and GetNumAutoQuestPopUps() == 0 then
					--if our tracker is empty
					otfHeader.Title:Show()
					if db.hideFrame then
						OTFContainer:SetAlpha(0)
						minimizeButton:EnableMouse(false)
					end
				elseif not WFWWPCDB.collapsed then	--if not empty and not collapsed
					otfHeader.Title:Hide()
					if db.hideFrame then
						OTFContainer:SetAlpha(1)
						minimizeButton:EnableMouse(true)
					end
				end
			else
				local index = ...
				if event == "QUEST_AUTOCOMPLETE" or index == 0 then
					if WFWWPCDB.collapsed then
						if IsInInstance() and db.instance then
							return
						else
							ExpandObjectiveTracker()
						end
					end
				end
			end
		end)
	
end

OTFContainer:RegisterEvent("PLAYER_ENTERING_WORLD")
OTFContainer:SetScript("OnEvent", function()
		Initialize()
		SetUpContainer()
	end)